package kr.aow.member.service;

import java.util.List;

import kr.aow.member.dao.IMemberDao;
import kr.aow.vo.MemberDTO;

public class MemberServiceImpl implements IMemberService {
	
	private static MemberServiceImpl service;
	private MemberServiceImpl(){}
	public static MemberServiceImpl getInstance(){
		if(service==null){
			service = new MemberServiceImpl();
		}
		return service;
	}
	
	private IMemberDao dao;
	
	public IMemberDao getDao() {
		return dao;
	}
	public void setDao(IMemberDao dao) {
		this.dao = dao;
	}
	
	
	@Override
	public MemberResult createMember(MemberDTO member) {
		String mb_id =	member.getMb_id();
		MemberDTO tempMember = dao.getMember(mb_id);
		if(tempMember==null){ // 사용가능한 아이디인 경우.
			int result = dao.insertMember(member);
			if(result>=1){
				return MemberResult.OK;
			}else{
				return MemberResult.Failed;
			}
		}else { // 아이디가 이미 사용중인 경우.
			return MemberResult.PKDuplicated;
		}
	}

	@Override
	public List<MemberDTO> getMemberList() {
		return dao.getMemberList();
	}

	@Override
	public MemberDTO getMember(String mb_id) {
		return dao.getMember(mb_id);
	}

	@Override
	public MemberResult modifyMember(MemberDTO member) {
		String mb_id = member.getMb_id();
		MemberDTO tempMember = dao.getMember(mb_id);
		if(tempMember==null){ // 입력한 아이디에 해당하는 회원이 존재하지 않음.
			return MemberResult.NOTEXIST;
		}else{
			if(tempMember.getMb_password().equals(member.getMb_password())){
				int result = dao.updateMember(member);
				if(result>=1){
					return MemberResult.OK;
				}else{
					return MemberResult.Failed;
				}
			}else{
				return MemberResult.INVALIDPASSWORD;
			}
		}
	}

	@Override
	public MemberResult removeMember(MemberDTO member) {
		String mb_id = member.getMb_id();
		MemberDTO tempMember = dao.getMember(mb_id);
		if(tempMember==null){
			return MemberResult.NOTEXIST;
		}else{
			if(tempMember.getMb_password().equals(member.getMb_password())){
				int result = dao.deleteMember(mb_id);
				if(result>=1){
					return MemberResult.OK;
				}else{
					return MemberResult.Failed;
				}
			}else{
				return MemberResult.INVALIDPASSWORD;
			}
		}
	}

}
